<?php
class mdl_account extends ujn_model {
	
	function login($data) {
		foreach($data as $k => $d) {
			$_SESSION[$k] = $d;
		}
	}
	
	function logout() {
		$_SESSION = array();
	}
	
	function get_current() {
		return $_SESSION;
	}
	
	
	function check_login($username, $userpswd) {
		$curr = $this->mysql()->find_one("select * from `v9_admin` where `username`='{$username}' limit 1;");
		if(!empty($curr) && md5(md5($userpswd).$curr['encrypt']) == $curr['password']) {
			$this->mysql()->update("update `v9_admin` set `lastloginip` = '{$_SERVER['REMOTE_ADDR']}', `lastlogintime` = unix_timestamp() where `userid` = '{$curr['userid']}' limit 1;");
			$this->login(array(
					'id' => $curr['userid'], 
					'uname' => $curr['realname'],
					'uname_nick' => $username,
					'urole' => $curr['roleid'],
					));
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @return mdl_account
	 */
	static function get_instance($empty = '') {
		return parent::get_instance(__CLASS__);
	}
}